<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Proxy实现负数索引</title>
</head>
<body>
<script>
    var ninjas = ['Samuel', 'ZhangBoy', 'Hannah', 'Yoshi', 'Hattori']

    function negaIndex(arr) {
        return new Proxy(arr, {
            get(target, p) {
                if (Number.isNaN(p)) {
                    return target[p]
                } else {
                    return target[p >= 0 ? p : target.length - Math.abs(p)]
                }
            },
            set(target, p, value) {
                if (Number.isNaN(p)) {
                    target[p] = value
                } else {
                    target[p >= 0 ? p : target.length - Math.abs(p)] = value
                }
            }
        })
    }

    ninjas = negaIndex(ninjas);
    console.log(ninjas[-2])
</script>
</body>
</html>
